package com.tc.juc.pack08;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.SynchronousQueue;

/**
 * @Auther: tianchao
 * @Date: 2021/10/8 22:56
 * @Description:
 */
@Slf4j
public class TestSynchronousQueue {

    public static void main(String[] args) throws InterruptedException {
        SynchronousQueue<Integer> integers = new SynchronousQueue<>();
        new Thread(() -> {
            try {
                log.debug("尝试putting {} ", 1);
                integers.put(1);
                log.debug("{} putted...", 1);
                log.debug("尝试putting...{} ", 2);
                integers.put(2);
                log.debug("{} putted...", 2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"放入线程").start();
        Thread.sleep(1000);
        new Thread(() -> {
            try {
                log.debug("尝试taking {}", 1);
                integers.take();
                log.debug("成功taking {}", 1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"消费-1").start();
        Thread.sleep(1000);
        new Thread(() -> {
            try {
                log.debug("尝试taking {}", 2);
                integers.take();
                log.debug("成功taking {}", 2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"消费-2").start();
    }
}
